﻿using System;
using System.Collections.Generic;
using System.Text;

namespace QT.Security
{
    /// <summary>
    /// 密码操作类
    /// </summary>
    public class Password
    {
        /// <summary>
        /// 生成指定长度的随机密码
        /// </summary>
        /// <param name="length">密码长度</param>
        /// <returns>string</returns>
        public static string Create(int length)
        {
            string pass = "";
            Random Ran = new Random();
            for (int i = 0; i < length; i++)
            {
                pass += ((char)Ran.Next(33, 127)).ToString();
            }
            return pass;
        }

        /// <summary>
        /// 生成指定长度的随机密码
        /// </summary>
        /// <param name="length">密码长度</param>
        /// <param name="number">是否包含数字</param>
        /// <param name="sign">是否包含特殊符号</param>
        /// <param name="smallword">是否包含小写字母</param>
        /// <param name="bigword">是否包含大写字母</param>
        /// <returns>string</returns>
        public static string Create(int length, bool number, bool sign, bool smallword, bool bigword)
        {
            Random Ran = new Random();
            int intA = 0;
            string pass = "";
            for (int i = 0; i < length; i++)
            {
                //生成随机数A，表示生成类型。1=数字，2=符号，3=小写字母，4=大写字母
                intA = Ran.Next(1, 5);
                if (intA == 1 && number)
                {
                    pass += Ran.Next(0, 10).ToString();
                    continue;
                }
                if (intA == 2 && sign)
                {
                    intA = Ran.Next(1, 5);
                    switch (intA)
                    {
                        case 1:
                            pass += ((char)(Ran.Next(33, 47))).ToString();
                            continue;
                        case 2:
                            pass += ((char)(Ran.Next(58, 65))).ToString();
                            continue;
                        case 3:
                            pass += ((char)(Ran.Next(91, 96))).ToString();
                            continue;
                        default:
                            pass += ((char)(Ran.Next(123, 127))).ToString();
                            continue;
                    }
                }
                if (intA == 3 && smallword)
                {
                    pass += ((char)(Ran.Next(97, 123))).ToString();
                    continue;
                }
                if (intA == 4 && smallword)
                {
                    pass += ((char)(Ran.Next(65, 91))).ToString();
                    continue;
                }
            }

            return pass;
        }
    }
}
